Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper package to easier use of Cocoa API with Go #163

Merged
merged 1 commit into from
Aug 3, 2023

Conversation

programmingkidx
Copy link
Contributor

This package is made to help the user more easily use the Cocoa API with Go. Currently it adds the SetAction() method. This method makes it very easy to set a control's action to a Go function. One feature that the current implementation of NSControl's SetAction() method doesn't implement that this method does is the ability to send an Action method the sender of the action. This is standard behavior in Objective-C.

This program can be used to test out this pull request. It displays a window with several controls. The user can click on a control and see a message printed to the terminal.

program
package main

import "github.com/progrium/macdriver/cocoa"
import "github.com/progrium/macdriver/objc"
import "github.com/progrium/macdriver/core"
import "github.com/progrium/macdriver/helper"
import "fmt"

func main() {
	
	app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) {
			win := cocoa.NSWindow_New()
			win.SetTitle("NSButton")
			winRect := core.NSMakeRect(20, 20, 700, 400)
			win.SetFrameDisplay(winRect, true)
			win.MakeKeyAndOrderFront(nil)

			/* NSSlider code */
			slider := cocoa.NSSlider_Alloc()
			sliderRect := core.NSMakeRect(30, 320, 60, 20)
			slider.InitWithFrame(sliderRect)
			//slider.SetMaxValue(100) // Needs the mapType() patch
			win.ContentView().AddSubview(slider)
			helper.SetAction(slider, doSliderAction)
			
			/* Textfield code */
			field := cocoa.NSTextField_Alloc()
			fieldRect := core.NSMakeRect(120, 320, 200, 20)
			field.InitWithFrame(fieldRect)
			field.SetStringValue("Click then push enter for action")
			win.ContentView().AddSubview(field)
			helper.SetAction(field, doTextFieldAction)
			
			/* NSButton code */
			buttonWidth := 70.0
			buttonHeight := 30.0
			for y := 0; y < 10; y++ {
				for x := 0; x < 10; x++ {
					rect := core.NSMakeRect(float64(x) * buttonWidth, float64(y) * buttonHeight, buttonWidth, buttonHeight)
					button := cocoa.NSButton_Alloc()
					button.InitWithFrame(rect)
					newTitle := fmt.Sprintf("%dx%d", x, y)
					button.SetTitle(newTitle)
					helper.SetAction(button, doButtonAction)
					win.ContentView().AddSubview(button)
				}
			}
		})

		app.SetActivationPolicy(cocoa.NSApplicationActivationPolicyRegular)
		app.ActivateIgnoringOtherApps(true)
		app.Run()
}

// all the buttons' action method
func doButtonAction(sender any) {
	button := sender.(cocoa.NSButton)
	fmt.Println("button pressed:", button.Title())
}

// the slider's action method
func doSliderAction(sender any) {
	slider := sender.(cocoa.NSSlider)
	fmt.Println("Slider:", slider.IntValue())
}

// the textfield's action method
func doTextFieldAction(sender any) {
	field := sender.(cocoa.NSTextField)
	fmt.Println("TextField:", field.StringValue())
}

@progrium
Copy link
Owner

I like it.

@progrium progrium merged commit 4ae7826 into progrium:main Aug 3, 2023
3 checks passed
@progrium
Copy link
Owner

progrium commented Aug 3, 2023

While this is rad it turns out we'll be picking up a very similar helper package in the rewrite branch with nearly identical API for usage. Here's an example: https://github.com/progrium/macdriver/blob/darwinkit/macos/_examples/widgets/main.go#L33

Merging this for the meantime in main, but this specific code won't make it to release since very little will!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants